package cn.agiledata.bank.transaction.audit.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.audit.exception.AuditException;
import cn.agiledata.bank.audit.service.AuditRuleService;
import cn.agiledata.bank.audit.service.AuditService;
import cn.agiledata.bank.common.dao.AccountTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AuditOperandType;
import cn.agiledata.bank.hibernateBean.EsPrePayTrans;
import cn.agiledata.bank.hibernateBean.Operator;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.dao.BatchTransSummaryDao;
import cn.agiledata.bank.transaction.dao.EsPrePayTransDao;
import cn.agiledata.bank.transaction.exception.TransactionException;
import cn.agiledata.bank.transaction.form.EsPrePayTransForm;
import cn.agiledata.bank.transaction.logic.BatchTransSummary;
import cn.agiledata.bank.transaction.logic.EsPrePayTransBean;
import cn.agiledata.bank.transaction.service.EsPrePayTransFactory;
import cn.agiledata.bank.transaction.service.GetEsPrePayTrans;
import cn.agiledata.bank.transaction.service.GetEsPrePayTransImpl;

import com.vnv.infrastructure.util.PagingResult;

/**
 * @author maxianbo
 * 
 */
public class EsPrePayTransServiceImpl implements EsPrePayTransService {
    private AccountTemplate accountTemplate;

    private EsPrePayTransDao esPrePayTransDao;

    private AuditService auditService;

    private BatchTransSummaryDao batchTransSummaryDao;
    
    private AuditRuleService raditRuleService;

    public void setAccountTemplate(AccountTemplate accountTemplate) {
        this.accountTemplate = accountTemplate;
    }

    public void setEsPrePayTransDao(EsPrePayTransDao esPrePayTransDao) {
        this.esPrePayTransDao = esPrePayTransDao;
    }

    public void setAuditService(AuditService auditService) {
        this.auditService = auditService;
    }

    public void setBatchTransSummaryDao(
            BatchTransSummaryDao batchTransSummaryDao) {
        this.batchTransSummaryDao = batchTransSummaryDao;
    }

    public AuditOperandType getAuditOperandType(String actionType) {
        return this.auditService.getAuditOperandType("ES_PRE_PAY_TRANS",
                actionType);
    }

    public boolean requiredAudit(long accountId, String actionType) {
        return this.auditService.requiredUserAudit(accountId,
                getAuditOperandType(actionType));
    }

    public void submitAuditOperation(EsPrePayTrans eppt, Operator operator)
            throws AuditException {
        // 根据交易类型获得审核类型
        String actionType = String.valueOf(eppt.getActionType().getId());
        AuditOperandType aot = getAuditOperandType(actionType);

        // 根据交易状态保存审核操作
        if (eppt.getTransStatus().equals(Constant.ES_TRANS_STATUS_UNAUDIT)) {
            // 待审核
            this.auditService.submit(aot, eppt.getId().longValue(), operator);
        } else if (eppt.getTransStatus().equals(
                Constant.ES_TRANS_STATUS_TOPUBMIT)) {
            // 待提交
            this.auditService.create(aot, eppt.getId().longValue(), operator);
        }

    }

    public EsPrePayTrans getEsPrePayTransById(long id) {
        return this.esPrePayTransDao.getEsPrePayTransById(new Long(id));
    }

    public EsPrePayTrans getEsPrePayTransByTransId(String transId) {
        return this.esPrePayTransDao.getEsPrePayTransByTransId(transId);
    }
    
    public EsPrePayTrans getEsPrePayTransByDebitOrderId(SubAccount debit,
            String orderId) {
        return this.esPrePayTransDao.getEsPrePayTransByDebitOrderId(debit,
                orderId);
    }

    public void submitAuditAccount(EsPrePayTrans eppt, Account account)
            throws AuditException {
        // 根据交易类型获得审核类型
        String actionType = String.valueOf(eppt.getActionType().getId());
        AuditOperandType aot = getAuditOperandType(actionType);

        // 根据交易状态保存审核操作
        if (eppt.getTransStatus().equals(Constant.ES_TRANS_STATUS_UNAUDIT)) {
            // 待审核
            this.auditService.submit(aot, eppt.getId().longValue(), account);
        } else if (eppt.getTransStatus().equals(
                Constant.ES_TRANS_STATUS_TOPUBMIT)) {
            // 待提交
            this.auditService.create(aot, eppt.getId().longValue(), account);
        }

    }

    public void saveOrUpdate(EsPrePayTrans eppt) {
        this.esPrePayTransDao.saveOrUpdate(eppt);

    }

    public EsPrePayTrans updateByType(EsPrePayTransForm eppt,
            HttpServletRequest request) {
        EsPrePayTrans tran = getEsPrePayTransById(eppt.getId().longValue());
        tran.setAmount(eppt.getAmount());
//        SubAccount debit = accountTemplate.getSubAccount(eppt.getDebitId());
//        tran.setDebit(debit);
        tran.setPurpose(eppt.getPurpose());
        tran.setCommodityInfo(eppt.getCommodityInfo());
        tran.setRemark(eppt.getRemark());
        // if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_D)){
        // SubAccount credit =
        // accountTemplate.getSubAccount(eppt.getCreditId());
        // tran.setCredit(credit);
        // }
        if (tran.getActionType().getId().toString().equals(
                Constant.ACTION_TYPE_C)) {
            // 此处增加商品信息
//            SubAccount credit = accountTemplate.getSubAccountByEmailMobile(eppt
//                    .getCreditMsg());
////            tran.setTradeType(eppt.getTradeType());
//            tran.setCredit(credit);
        }
        if (tran.getActionType().getId().toString().equals(
                Constant.ACTION_TYPE_G))
        {
            //tran.setAccountBankSeq(eppt.getAccountBankSeq());
        	tran.setBankSeq(new Long(eppt.getAccountBankSeq()));
            tran.setCardNo(eppt.getCardNo());
            tran.setBankName(eppt.getBankName());
            tran.setBankCitySeq(eppt.getBankCitySeq());
            tran.setBankProvSeq(eppt.getBankProvSeq());
            tran.setPersonMail(eppt.getPersonMail());
            tran.setPersonMobile(eppt.getPersonMobile());
            tran.setPersonName(eppt.getPersonName());
            tran.setPersonPhone(eppt.getPersonPhone());
        }
        AccountSummary accountsummary = WeatherUtil
                .getLoingInfoFromSession(request);
        String actionType = String.valueOf(tran.getActionType().getId());
        AuditOperandType aot = getAuditOperandType(actionType);
        boolean ifAudit = this.auditService.requiredUserAudit(accountsummary
                .getAccountId(), aot);
        if (ifAudit) {
            tran.setTransStatus(Constant.ES_TRANS_STATUS_UNAUDIT);
        } else {
            tran.setTransStatus(Constant.ES_TRANS_STATUS_UNPAY);
        }
        WeatherUtil.fillCreateAndUpdateInfo(tran.getId(), WeatherUtil.getOperator(request).getId().toString());
        saveOrUpdate(tran);
        return tran;
    }
    
    public EsPrePayTrans updateStatus( EsPrePayTrans tran,
    		HttpServletRequest request) {
    	  String actionType = String.valueOf(tran.getActionType().getId());
    	  AccountSummary accountsummary = WeatherUtil
          .getLoingInfoFromSession(request);
          AuditOperandType aot = getAuditOperandType(actionType);
          boolean ifAudit = this.auditService.requiredUserAudit(accountsummary
                  .getAccountId(), aot);
          if (ifAudit) {
              tran.setTransStatus(Constant.ES_TRANS_STATUS_UNAUDIT);
          } else {
              tran.setTransStatus(Constant.ES_TRANS_STATUS_UNPAY);
          }
          WeatherUtil.fillCreateAndUpdateInfo(tran.getId(), WeatherUtil.getOperator(request).getId().toString());
          saveOrUpdate(tran);
          return tran;
    }
    
    
    public EsPrePayTrans updateByTypeWithoutStatus(EsPrePayTransForm eppt,
            HttpServletRequest request) {
        EsPrePayTrans tran = getEsPrePayTransById(eppt.getId().longValue());
        tran.setAmount(eppt.getAmount());
        tran.setCommodityInfo(eppt.getCommodityInfo());
//        SubAccount debit = accountTemplate.getSubAccount(eppt.getDebitId());
//        tran.setDebit(debit);
        tran.setPurpose(eppt.getPurpose());
        tran.setRemark(eppt.getRemark());
        // if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_D)){
        // SubAccount credit =
        // accountTemplate.getSubAccount(eppt.getCreditId());
        // tran.setCredit(credit);
        // }
        if (tran.getActionType().getId().toString().equals(
                Constant.ACTION_TYPE_C)) {
            // 此处增加商品信息
//            SubAccount credit = accountTemplate.getSubAccountByEmailMobile(eppt
//                    .getCreditMsg());
////            tran.setTradeType(eppt.getTradeType());
//            tran.setCredit(credit);
        }
        if (tran.getActionType().getId().toString().equals(
                Constant.ACTION_TYPE_G)) ;
        {
            //tran.setAccountBankSeq(eppt.getAccountBankSeq());
            tran.setCardNo(eppt.getCardNo());
            tran.setBankName(eppt.getBankName());
            tran.setBankCitySeq(eppt.getBankCitySeq());
            tran.setBankProvSeq(eppt.getBankProvSeq());
            tran.setPersonMail(eppt.getPersonMail());
            tran.setPersonMobile(eppt.getPersonMobile());
            tran.setPersonName(eppt.getPersonName());
            tran.setPersonPhone(eppt.getPersonPhone());
            
        }
        AccountSummary accountsummary = WeatherUtil
                .getLoingInfoFromSession(request);
        WeatherUtil.fillCreateAndUpdateInfo(tran.getId(), accountsummary.getAccount().getId().toString());
        saveOrUpdate(tran);
        return tran;
    }
    
    public boolean enoughDebitBalance(EsPrePayTrans esPrePayTrans) {
        SubAccount debit = esPrePayTrans.getDebit();
        double balance = debit.getAccountBalance().getBalance().doubleValue();
        double amount = esPrePayTrans.getAmount().doubleValue();
        double debitFee = esPrePayTrans.getDebitFee().doubleValue();
        return balance >= amount + debitFee;
    }

    public void deleteEsPrePayTrans(long id) {
        EsPrePayTrans eppt = getEsPrePayTransById(id);
        eppt.setTransStatus(Constant.ES_TRANS_STATUS_CANCEL);
        saveOrUpdate(eppt);
    }

    public void createEsPrePayTrans(Transaction trans, Account account,
            Operator operator) throws AuditException {
        EsPrePayTrans esPPTran = new EsPrePayTrans();
        BeanUtils.copyProperties(trans, esPPTran);
        // PropertyUtils.copyProperties(esPPTran, trans);
        // 判断是否需要审核
        if (requiredAudit(account.getId().longValue(), trans.getActionType()
                .getId().toString())) {
            esPPTran.setTransStatus(Constant.ES_TRANS_STATUS_UNAUDIT);
        } else {
            esPPTran.setTransStatus(Constant.ES_TRANS_STATUS_UNPAY);
        }
        // 交易生成源
        if (operator == null) {
            esPPTran.setSource(EsPrePayTrans.SOURCE_ACCOUNT);
        } else {
            esPPTran.setSource(EsPrePayTrans.SOURCE_OPERATOR);
        }
        // 保存交易对象
        GetEsPrePayTrans getEsPrePayTran = new GetEsPrePayTransImpl();
        EsPrePayTransFactory epptf = getEsPrePayTran.getTransaction(trans
                .getActionType().getId().toString());
        epptf.saveTransaction(esPPTran);
        // 保存审核对象
        if (operator != null) {
            submitAuditOperation(esPPTran, operator);
        } else {
            submitAuditAccount(esPPTran, account);
        }
    }

    public PagingResult getEsPrePayTransListByBatchNo(String batchNo,
            int pageNo, int pageSize) {
        return this.esPrePayTransDao.getEsPrePayTransListByBatchNo(batchNo,
                pageNo, pageSize);
    }

    private PageBean toPageBean(PagingResult pr) {
        PageBean pb = new PageBean();
        pb.setData(pr.getRecords());
        pb.setCurPage(pr.getPageNo());
        pb.setLength(pr.getPageSize());
        pb.setNext(pr.getNext());
        pb.setPrevious(pr.getPrevious());
        pb.setTotalCount(pr.getTotalCount());
        pb.setTotalPage(pr.getPageCount());
        return pb;
    }

    public PageBean getBatchTransSummaryList(EsPrePayTransBean bean) {
        int pageNo = Integer.parseInt(bean.getStartPage());
        int pageSize = Constant.PAGESIZE.intValue();
        PagingResult pr = this.batchTransSummaryDao
                .getBatchTransSummaryListWithoutCancel(bean, pageNo, pageSize);
        if (pr.getRecordCount() > 0) {
            // 获取合计数据
            List records = pr.getRecords();
            List batchNoList = new ArrayList(records.size());
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                batchNoList.add(bts.getBatchNo());
            }
            BatchTransSummary[] btss = this.batchTransSummaryDao
                    .getBatchTransSummaryTotalList(batchNoList);
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                for (int j = 0; j < btss.length; j++) {
                    if (btss[j].getBatchNo().equals(bts.getBatchNo())) {
                        bts.setTotalAmount(btss[j].getTotalAmount());
                        bts.setTotalFee(btss[j].getTotalFee());
                        bts.setTotalCount(btss[j].getTotalCount());
                        break;
                    }
                }
            }
        }
        return toPageBean(pr);
    }
    
    public PageBean getBatchAuditingTransSummaryList(EsPrePayTransBean bean) {
        int pageNo = Integer.parseInt(bean.getStartPage());
        int pageSize = Constant.PAGESIZE.intValue();
        PagingResult pr = this.batchTransSummaryDao
                .getBatchAuditingTransSummaryList(bean, pageNo, pageSize);
        if (pr.getRecordCount() > 0) {
            // 获取合计数据
            List records = pr.getRecords();
            List batchNoList = new ArrayList(records.size());
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                batchNoList.add(bts.getBatchNo());
            }
            BatchTransSummary[] btss = this.batchTransSummaryDao
                    .getBatchTransSummaryTotalList(batchNoList);
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                for (int j = 0; j < btss.length; j++) {
                    if (btss[j].getBatchNo().equals(bts.getBatchNo())) {
                        bts.setTotalAmount(btss[j].getTotalAmount());
                        bts.setTotalFee(btss[j].getTotalFee());
                        bts.setTotalCount(btss[j].getTotalCount());
                        break;
                    }
                }
            }
        }
        return toPageBean(pr);
    }
    
    public PageBean getBatchAuditTransSummaryList(EsPrePayTransBean bean) {
        int pageNo = Integer.parseInt(bean.getStartPage());
        int pageSize = Constant.PAGESIZE.intValue();
        PagingResult pr = this.batchTransSummaryDao
                .getBatchTransSummaryListWithoutCancel(bean, pageNo, pageSize);
        if (pr.getRecordCount() > 0) {
            // 获取合计数据
            List records = pr.getRecords();
            List batchNoList = new ArrayList(records.size());
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                batchNoList.add(bts.getBatchNo());
            }
            BatchTransSummary[] btss = this.batchTransSummaryDao
                    .getBatchTransSummaryTotalList(batchNoList);
            for (int i = 0; i < records.size(); i++) {
                BatchTransSummary bts = (BatchTransSummary) records.get(i);
                for (int j = 0; j < btss.length; j++) {
                    if (btss[j].getBatchNo().equals(bts.getBatchNo())) {
                        bts.setTotalAmount(btss[j].getTotalAmount());
                        bts.setTotalFee(btss[j].getTotalFee());
                        bts.setTotalCount(btss[j].getTotalCount());
                        break;
                    }
                }
            }
        }
        return toPageBean(pr);
    }

    public BatchTransSummary getBatchTransSummaryTotal(String batchNo) {
        return this.batchTransSummaryDao.getBatchTransSummaryTotal(batchNo);
    }

    public BatchTransSummary[] getBatchTransSummaryListGroupByTransStatus(
            String batchNo) {
        return this.batchTransSummaryDao
                .getBatchTransSummaryListGroupByTransStatus(batchNo);
    }

    public boolean batchEsPrePayTransCanBePaid(String batchNo) {
        List esPrePayTransList = this.esPrePayTransDao
                .getEsPrePayTransListByBatchNo(batchNo);
        boolean hasUnpay = false; // 是否存在待支付交易
        for (int i = 0; i < esPrePayTransList.size(); i++) {
            EsPrePayTrans esTrans = (EsPrePayTrans) esPrePayTransList.get(i);
            String transStatus = esTrans.getTransStatus();
            // 存在待支付交易，直接进入下一个循环步骤
            if (Constant.ES_TRANS_STATUS_UNPAY.equals(transStatus)) {
                hasUnpay = true;
                continue;
            }
            // 此时如果不为被取消交易，则该交易处于审核流程中，不能进行支付
            if (!Constant.ES_TRANS_STATUS_CANCEL.equals(transStatus)) {
                return false;
            }
        }
        return hasUnpay;
    }

    /**
     * 校验批量支付等待最终支付的交易清单
     * 
     * @throws TransactionException
     */
    private void validateWaitingBatchPayTrans(List esPrePayTransList)
            throws TransactionException {
        SubAccount debit = null;
        BigDecimal total = new BigDecimal(0.00);
        for (int i = 0; i < esPrePayTransList.size(); i++) {
            EsPrePayTrans esTrans = (EsPrePayTrans) esPrePayTransList.get(i);
            String transStatus = esTrans.getTransStatus();
            if (Constant.ES_TRANS_STATUS_UNPAY.equals(transStatus)) {
                if (debit == null) { // 付款方子账户为空时才赋值，避免重复赋值
                    debit = esTrans.getDebit();
                }
                BigDecimal amount = new BigDecimal(esTrans.getAmount()
                        .doubleValue());
                BigDecimal fee = new BigDecimal(esTrans.getDebitFee()
                        .doubleValue());
                total = total.add(amount).add(fee); // 计算待支付总金额
                continue; // 检查下一条交易
            }
            // 此时如果不为被取消交易，则该交易处于审核流程中，不能进行支付
            if (!Constant.ES_TRANS_STATUS_CANCEL.equals(transStatus)) {
                throw TransactionException.BATCH_AUDITING_TRANS_CANNOT_PAY;
            }
        }
        // 此时付款方子账户为空说明没有待支付交易，不能进行支付
        if (debit == null) {
            throw TransactionException.BATCH_NO_TRANS_WAITING_PAY;
        }
        // 判断余额
        if (debit.getAccountBalance().getBalance().doubleValue() < total
                .doubleValue()) {
            throw TransactionException.LACK_OF_BALANCE;
        }
    }

    public void batchPayEsPrePayTrans(String batchNo)
            throws TransactionException {
        List esPrePayTransList = this.esPrePayTransDao
                .getEsPrePayTransListByBatchNo(batchNo);
        validateWaitingBatchPayTrans(esPrePayTransList);
        GetEsPrePayTrans getEsPrePayTran = new GetEsPrePayTransImpl();
        for (int i = 0; i < esPrePayTransList.size(); i++) {
            EsPrePayTrans esPrePayTrans = (EsPrePayTrans) esPrePayTransList
                    .get(i);
            String transStatus = esPrePayTrans.getTransStatus();
            // 只有待支付交易才进行最终支付
            String actionType = String.valueOf(esPrePayTrans
            		.getActionType().getId());
            //退款交易的新增处理逻辑:
            //当交易类型为退款.但bank_seq为空的时候.说明该条交易之前为账户间交易. 直接走账户间交易流程.
            //这里通过临时改变actiontype来实现
            if (Constant.ACTION_TYPE_R.equals(actionType) && esPrePayTrans.getBankSeq() == null){
            	actionType = Constant.ACTION_TYPE_C;
            }
            //end
            
            if (Constant.ES_TRANS_STATUS_UNPAY.equals(transStatus)) {
                EsPrePayTransFactory factory = getEsPrePayTran
                        .getTransaction(actionType);
                // 进行实际支付前再次进行余额检查
                if (!enoughDebitBalance(esPrePayTrans)) {
                    throw TransactionException.LACK_OF_BALANCE;
                }
                if (Constant.ACTION_TYPE_Y.equals(actionType)) {
                	factory.updateTransaction(esPrePayTrans);
                } else {
                	factory.esPrePayTransToTransaction(esPrePayTrans);
                }
            }
        }
    }
    
    public List getEsPrePayTransListByBatchNo(String batchNo) {
    	return this.esPrePayTransDao
        .getEsPrePayTransListByBatchNo(batchNo);
    }

    public BatchTransSummary[] getBatchTransSummaryListOnlyBatchNo(Account debit,
            String transStatus) {
        return this.batchTransSummaryDao.getBatchTransSummaryListOnlyBatchNo(debit,
                transStatus);
    }

	public List getEsPrePayTranses(String batchNo) {
		return this.esPrePayTransDao.getEsPrePayTransListByBatchNo(batchNo);
	}
	
    public void setSummary( List summary,Operator oper){
		Iterator it = summary.iterator();
		while (it.hasNext()) {
			BatchTransSummary totalSummary = (BatchTransSummary) it.next();
			BatchTransSummary rejectSummary = batchTransSummaryDao.getBatchTransSummaryByTransStatus(totalSummary.getBatchNo(), Constant.ES_TRANS_STATUS_REJECTED);
			if(rejectSummary!=null)
			totalSummary.setRejectCount(new Long(rejectSummary.getStatusCount()).intValue());
			else
				totalSummary.setRejectCount(0);
			int required = requiredAudit(totalSummary.getBatchNo(),oper);
			totalSummary.setWaitAuditCount(required);
			totalSummary.setAudit((required>0)+"");
		}
	}
    
//    /**
//     * 根据某一笔临时交易，及审核员判断该审核员在该审核规则中的第几步
//     * @param EsPrePayTrans 要判断的临时交易
//     * @param Operator 审核员
//     * @return
//     */
//     public int getCurentStep(EsPrePayTrans trans,Operator op){
//    	int curentStep = -1;
//		RecentAuditOperation oper = auditService.getRecentAuditOperation(
//				getAuditOperandType(trans.getActionType().getId()
//						.toString()), trans.getId().longValue());
//		AuditRule rule = oper.getAuditRule();
//		Set steps = rule.getAuditSteps();
//		Iterator it = steps.iterator();
//		while(it.hasNext()){
//			AuditStep step = (AuditStep) it.next();
//			AuditGroup group = step.getAuditGroup();
//			Set auditors = group.getOperators();
//			if(auditors.contains(op)){
//				return  step.getLevel();
//			}
//		}
//		return curentStep;
//     }

     private int requiredAudit(String batchNo,Operator op){
		List batchTranses = esPrePayTransDao
				.getEsPrePayTransListByBatchNo(batchNo);
		Iterator it = batchTranses.iterator();
		int required = 0;
		while (it.hasNext()) {
			EsPrePayTrans tran = (EsPrePayTrans) it.next();
			if(Constant.ES_TRANS_STATUS_CANCEL.equals(tran.getTransStatus())){
    			continue;
    		}
			if(auditService.hasAuditAuthority(getAuditOperandType(tran
							.getActionType().getId().toString()), tran.getId().longValue(), op))
				required = required+1;
		}
		return required;
	}
    
    public boolean ifAllAudited(String batchNo,Operator oper){
    	List esPrePayTranses  = esPrePayTransDao.getEsPrePayTransListByBatchNo(batchNo);
    	boolean audited = true;
    	Iterator it = esPrePayTranses.iterator();
    	while(it.hasNext()){
    		EsPrePayTrans trans = (EsPrePayTrans) it.next();
    		if(Constant.ES_TRANS_STATUS_CANCEL.equals(trans.getTransStatus())){
    			continue;
    		}
    		if(Constant.ES_TRANS_STATUS_REJECTED.equals(trans.getTransStatus())){
    			return false;
    		}
    		  if(!auditService.hasAuditAuthority(getAuditOperandType(trans.getActionType().getId().toString()), trans.getId().longValue(), oper)){
              	return false;
              }
    	}
    	return audited;
    }

	public PageBean getBatchAuditeRejectTransSummaryList(EsPrePayTransBean bean) {
		 int pageNo = Integer.parseInt(bean.getStartPage());
	        int pageSize = Constant.PAGESIZE.intValue();
	        PagingResult pr = this.batchTransSummaryDao
	                .getBatchAuditingTransSummaryList(bean, pageNo, pageSize);
	        if (pr.getRecordCount() > 0) {
	            // 获取合计数据
	            List records = pr.getRecords();
	            List batchNoList = new ArrayList(records.size());
	            for (int i = 0; i < records.size(); i++) {
	                BatchTransSummary bts = (BatchTransSummary) records.get(i);
	                batchNoList.add(bts.getBatchNo());
	            }
	            BatchTransSummary[] btss = this.batchTransSummaryDao
	                    .getBatchTransSummaryTotalList(batchNoList);
	            for (int i = 0; i < records.size(); i++) {
	                BatchTransSummary bts = (BatchTransSummary) records.get(i);
	                for (int j = 0; j < btss.length; j++) {
	                    if (btss[j].getBatchNo().equals(bts.getBatchNo())) {
	                        bts.setTotalAmount(btss[j].getTotalAmount());
	                        bts.setTotalFee(btss[j].getTotalFee());
	                        bts.setTotalCount(btss[j].getTotalCount());
	                        break;
	                    }
	                }
	            }
	        }
	        return toPageBean(pr);
	}
	
	public void createPreReturnTrans(EsPrePayTrans esPPTran, Account account,
            Operator operator) throws AuditException {
        // 判断是否需要审核
        if (requiredAudit(account.getId().longValue(), esPPTran.getActionType()
                .getId().toString())) {
            esPPTran.setTransStatus(Constant.ES_TRANS_STATUS_UNAUDIT);
        } else {
            esPPTran.setTransStatus(Constant.ES_TRANS_STATUS_UNPAY);
        }
        
        // 交易生成源
        if (operator == null) {
            esPPTran.setSource(EsPrePayTrans.SOURCE_ACCOUNT);
        } else {
            esPPTran.setSource(EsPrePayTrans.SOURCE_OPERATOR);
        }
        
        // 保存交易对象
        GetEsPrePayTrans getEsPrePayTran = new GetEsPrePayTransImpl();
        EsPrePayTransFactory epptf = getEsPrePayTran.getTransaction(esPPTran
                .getActionType().getId().toString());
        epptf.saveTransaction(esPPTran);
        // 保存审核对象
        if (operator != null) {
            submitAuditOperation(esPPTran, operator);
        } else {
            submitAuditAccount(esPPTran, account);
        }
    }
}